package cn.dgut.training.service.impl;

import cn.dgut.training.controller.ActivityService;
import cn.dgut.training.controller.GoodsService;
import cn.dgut.training.dto.GroupBuyingRulesDTO;
import cn.dgut.training.emun.GroupRulesStatusEnum;
import cn.dgut.training.entity.activity.GroupBuying;
import cn.dgut.training.entity.activity.GroupBuyingRules;
import cn.dgut.training.entity.goods.Goods;
import cn.dgut.training.entity.goods.GoodsProduct;
import cn.dgut.training.mapper.GroupBuyingRulesMapper;
import cn.dgut.training.service.IGroupBuyingRulesService;
import cn.dgut.training.utils.RestUtil;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.GroupBuyingRulesVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.security.acl.Group;

@Service
public class GroupBuyingRulesServiceImpl extends ServiceImpl<GroupBuyingRulesMapper, GroupBuyingRules> implements IGroupBuyingRulesService {

    @Resource
    private GroupBuyingRulesMapper groupBuyingRulesMapper;

    @Autowired
    private GoodsService goodsService;

    @Override
    public GroupBuyingRulesVO createRules(GroupBuyingRulesDTO groupBuyingRulesDTO){

        GroupBuyingRules groupBuyingRules = new GroupBuyingRules();
        GroupBuyingRulesVO groupBuyingRulesVO = new GroupBuyingRulesVO();
        BeanUtils.copyProperties(groupBuyingRulesDTO,groupBuyingRules);

        CommonResponse commonResponse=goodsService.findGoodsByName(groupBuyingRules.getGoodsName());
        Goods g = (Goods) RestUtil.getObject(commonResponse,Goods.class);
        CommonResponse commonResponse1 = goodsService.findGoodsProductById(groupBuyingRules.getProductId());
        GoodsProduct gp = (GoodsProduct) RestUtil.getObject(commonResponse1,GoodsProduct.class);

        groupBuyingRules.setGoodsId(g.getId());
        groupBuyingRules.setProductId(groupBuyingRulesDTO.getProductId());
        groupBuyingRules.setProductNumber(groupBuyingRulesDTO.getProductNumber());
        groupBuyingRules.setSpecifications(groupBuyingRulesDTO.getSpecifications());

        if(!StringUtils.isBlank(gp.getUrl())){
            groupBuyingRules.setPicUrl(gp.getUrl());
        }else{
            groupBuyingRules.setPicUrl(g.getPicUrl());
        }

        long date1 = groupBuyingRules.getExpireTime().getTime();
        long date2 = DateTime.now().getTime();
        if(date1 < date2){
            groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_EXPIRELINE.getValue());
        }else{
            groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_ONLINE.getValue());
        }

        groupBuyingRulesMapper.insert(groupBuyingRules);

        BeanUtils.copyProperties(groupBuyingRules,groupBuyingRulesVO);

        return groupBuyingRulesVO;
    }

    @Override
    public GroupBuyingRulesVO editRules(GroupBuyingRulesDTO groupBuyingRulesDTO){

        GroupBuyingRules groupBuyingRules = new GroupBuyingRules();
        BeanUtils.copyProperties(groupBuyingRulesDTO,groupBuyingRules);

        CommonResponse commonResponse=goodsService.findGoodsByName(groupBuyingRules.getGoodsName());
        Goods g = (Goods) RestUtil.getObject(commonResponse,Goods.class);
        CommonResponse commonResponse1 = goodsService.findGoodsProductById(groupBuyingRules.getProductId());
        GoodsProduct gp = (GoodsProduct) RestUtil.getObject(commonResponse1,GoodsProduct.class);

        groupBuyingRules.setGoodsId(g.getId());
        groupBuyingRules.setProductId(groupBuyingRulesDTO.getProductId());
        groupBuyingRules.setProductNumber(groupBuyingRulesDTO.getProductNumber());
        groupBuyingRules.setSpecifications(groupBuyingRulesDTO.getSpecifications());

        if(!StringUtils.isBlank(gp.getUrl())){
            groupBuyingRules.setPicUrl(gp.getUrl());
        }else{
            groupBuyingRules.setPicUrl(g.getPicUrl());
        }

        long date1 = groupBuyingRules.getExpireTime().getTime();
        long date2 = DateTime.now().getTime();
        if(date1 < date2){
            groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_EXPIRELINE.getValue());
        }else{
            groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_ONLINE.getValue());
        }

        groupBuyingRulesMapper.updateById(groupBuyingRules);

        GroupBuyingRulesVO groupBuyingRulesVO = new GroupBuyingRulesVO();
        BeanUtils.copyProperties(groupBuyingRules,groupBuyingRulesVO);

        return groupBuyingRulesVO;
    }

    @Override
    public GroupBuyingRulesVO listGroupBuyingRulesByPage(GroupBuyingRulesDTO groupBuyingRulesDTO){
        //构造分页
        IPage ipage = new Page();
        BeanUtils.copyProperties(groupBuyingRulesDTO, ipage);
        //获取条件查询构造器
        QueryWrapper queryWrapper = GroupBuyingRulesDTO.getWrapper(groupBuyingRulesDTO);

        IPage res = groupBuyingRulesMapper.selectPage(ipage, queryWrapper);

        GroupBuyingRulesVO groupBuyingRulesVO = new GroupBuyingRulesVO(res);

        return groupBuyingRulesVO;
    }

    @Override
    public GroupBuyingRulesVO deleteRules(GroupBuyingRulesDTO groupBuyingRulesDTO){

        GroupBuyingRules groupBuyingRules = new GroupBuyingRules();
        BeanUtils.copyProperties(groupBuyingRulesDTO,groupBuyingRules);

        groupBuyingRules.setStatus(GroupRulesStatusEnum.RULES_FOR_OFFLINE.getValue());
        groupBuyingRulesMapper.updateById(groupBuyingRules);

        groupBuyingRulesMapper.deleteById(groupBuyingRules);

        GroupBuyingRulesVO groupBuyingRulesVO = new GroupBuyingRulesVO();
        BeanUtils.copyProperties(groupBuyingRules,groupBuyingRulesVO);

        return groupBuyingRulesVO;
    }
}
